package com.by.common.controller;

import com.by.common.bean.ResultBean;
import com.by.common.exception.BusinessException;
import com.by.common.exception.ValidateException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLSyntaxErrorException;

/**
 * 全局异常处理
 */
@RestControllerAdvice //异常的增强注解
public class GlobalExceptionController extends BaseController{
    /**
     * 当系统中 抛出了ValidateException 异常  执行该方法
     * @param e
     * @return
     */
    @ExceptionHandler(ValidateException.class)
    public ResultBean handlerValidateException(ValidateException e){
        return  fail("验证失败",e.getErrors());
    }
    
    /**
     * 处理sql语法异常
     * @param e
     * @return
     */
    @ExceptionHandler(SQLSyntaxErrorException.class)
    public ResultBean handlerSQLSyntaxErrorException(SQLSyntaxErrorException e){
        return fail("SQL语法错误，请联系工程师");
    }
    
    
    
    @ExceptionHandler(BusinessException.class)
    public ResultBean handlerBusinessException(BusinessException e){
        e.printStackTrace();
        return fail(e.getMessage());
    }
    
    
    
    
//    /**
//     * 异常父类，接收后台全局异常
//     * @param e
//     * @return
//     */
//
////    @ExceptionHandler(Exception.class)
////    public ResultBean handlerException(Exception e){
////        return  fail("后台异常");
////    }
}
